// Scroller.cpp: implementation of the Scroller class.
//
//////////////////////////////////////////////////////////////////////

//14-01-2000 jd	16250	silly typo
//23-11-2000 jd	data grid development

#include "stdafx.h"
#include "Scroller.h"
#include "ScrollerLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(Scroller, AWTPanel)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Scroller::Scroller()
{
	m_bTrack = TRUE;
	setLayout(new ScrollerLayout(this));
	add("Scroll", m_viewport = new AWTPanel());
	add("East", m_vbar = new AWTScrollbar(AWTScrollbar::VERTICAL));
	add("South", m_hbar = new AWTScrollbar(AWTScrollbar::HORIZONTAL));

	m_hbar->addAdjustmentListener(this);
	m_vbar->addAdjustmentListener(this);
}

Scroller::~Scroller()
{

}

void Scroller::adjustmentValueChanged(AWTAdjustmentEvent* event) 
{
	switch(event->getAdjustmentType()) 
	{
		case AWTAdjustmentEvent::BLOCK_INCREMENT:  
			scrollLineUp(); 
			break;
		case AWTAdjustmentEvent::BLOCK_DECREMENT:  
			scrollLineDown(); 
			break;
		case AWTAdjustmentEvent::UNIT_INCREMENT:   
			scrollPageUp(); 
			break;
		case AWTAdjustmentEvent::UNIT_DECREMENT:   
			scrollPageDown(); 
			break;
		case AWTAdjustmentEvent::TRACK:            
			if (m_bTrack)
				scrollAbsolute(); 
			break;
		case AWTAdjustmentEvent::ENDTRACK:            
			scrollAbsolute(); 
			break;
	}
	validate();
}

void Scroller::manageScrollbars()
{ 
	manageHorizontalScrollbar();
	manageVerticalScrollbar  ();
}

void Scroller::manageHorizontalScrollbar() 
{
	CSize size = getSize();
	CSize scrollAreaSize = getScrollAreaSize();

	if(m_vbar->isVisible())
		size.cx -= m_vbar->getSize().cx;

	if(scrollAreaSize.cx > size.cx) 
	{
		if( !m_hbar->isVisible())
			m_hbar->setVisible(TRUE);
	}
	else if(m_hbar->isVisible() && (m_hbar->getValue() == 0)) 
	{
		m_hbar->setVisible(FALSE);
		m_hbar->setValue(0);
		m_viewport->invalidate();
//		validate();
//		repaint();
	}
}

void Scroller::manageVerticalScrollbar() 
{
  CSize size = getSize();
  CSize scrollAreaSize = getScrollAreaSize();

  if(m_hbar->isVisible())
    size.cy -= m_hbar->getSize().cy;

  if(scrollAreaSize.cy > size.cy) 
	{
    if( ! m_vbar->isVisible())
      m_vbar->setVisible(TRUE);
  }
  else if(m_vbar->isVisible() && (m_vbar->getValue() == 0)) 
	{
    m_vbar->setVisible(FALSE);
    m_vbar->setValue(0);
		m_viewport->invalidate();
//		validate();
//    repaint();
  }
}

void Scroller::setScrollPosition(int x, int y) 
{
///////////////////////////////////
// jd 23/11/00 - data grid work

/*
	scrollTo(x,y);
	m_hbar->setValue(x);
//	m_hbar->setValue(y); doh! - jd
	m_vbar->setValue(y);   //jd 14/1/2000
	repaint();
*/

	m_hbar->setValue(x);
	m_vbar->setValue(y);
	scroll();

//jd end
///////////////////////////////////
}

void Scroller::setScrollbarValues() 
{
  if(m_hbar->isVisible()) setHorizontalScrollbarValues();
  if(m_vbar->isVisible()) setVerticalScrollbarValues();
}

void Scroller::setHorizontalScrollbarValues() 
{
  CSize vsize = m_viewport->getSize();
  CSize scrollAreaSize = getScrollAreaSize(); 
  int max = scrollAreaSize.cx; 

  m_hbar->setValues(m_hbar->getValue(), // value
                    vsize.cx,           // amt visible/page
                    0,                  // minimum
                    max);               // maximum

  setHorizontalLineAndPageIncrements();
}

void Scroller::setVerticalScrollbarValues() 
{
	CSize vsize = m_viewport->getSize();
	CSize scrollAreaSize = getScrollAreaSize(); 
	int max = scrollAreaSize.cy;

	m_vbar->setValues(m_vbar->getValue(), // value
									  vsize.cy,    // amt visible/page
									  0,               // minimum
									  max);            // maximum

	setVerticalLineAndPageIncrements();
}

void Scroller::setHorizontalLineAndPageIncrements() 
{
  CSize vsize = m_viewport->getSize();
 CSize size = getScrollAreaSize() - vsize;
  m_hbar->setUnitIncrement(size.cx/10);
  m_hbar->setBlockIncrement(size.cx/5);
}

void Scroller::setVerticalLineAndPageIncrements() 
{
  CSize vsize = m_viewport->getSize();
  CSize size = getScrollAreaSize() - vsize;
  m_vbar->setUnitIncrement(size.cy/10);
  m_vbar->setBlockIncrement(size.cy/5);
}
